<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <div class="group2">
          <img src="../../assets/img/logo.png" style="width:100%;height:100%" alt="">
        </div>
        <h3 class="title">牡蛎自习室</h3>
      </div>

      <el-form-item prop="account">
        <!-- <span class="svg-container">
          <svg-icon icon-class="user" />
        </span> -->
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="请输入账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <!-- <span class="svg-container">
          <svg-icon icon-class="password" />
        </span> -->
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" v-if="passwordType === 'password'" @click="showPwd">
          <!-- 获取验证码 -->
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
        <span class="show-pwd" v-else @click="showPwd">
          <!-- 获取验证码 -->
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="warning"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span>登录即表示你已同意服务条款和法律声明</span>
        <!-- <span>password: any</span> -->
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from "@/utils/validate";
import { adminStore } from "@/api/menu";
import Base64 from "@/utils/base"
import { UserMenus } from '@/api/menu'
	//加密方法
export default {
  name: "Login",
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error("请输入正确的用户名"));
    //   } else {
    //     callback();
    //   }
    // };
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error("密码不能少于6位"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      loginForm: {
        account: "",
        password: ""
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur",  message: '请输入账号' }
        ],
        password: [
          { required: true, trigger: "blur", message: '请输入密码' }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let parms = Base64.encode(JSON.stringify(this.loginForm))
          this.loading = true;
          this.$store
            .dispatch("user/login", {request_data: parms})
            .then((res) => {
            //   adminStore({
            //   request_data: Base64.encode(JSON.stringify({store_id:""}))
            // }).then((response) => {
            //   localStorage.setItem("storemenu",JSON.stringify(response.data))
            // })
              this.$router.push({ path: '/' });
              // localStorage.setItem('path', path)
              this.loading = false;
            })
            .catch((err) => {
              if(String(err).indexOf('密码错误，请重新输入')!=-1){
                this.loginForm.password = ""
              }
              if(String(err).indexOf('账号不存在，请检查')!=-1){
                this.$refs["loginForm"].resetFields()
              }
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    // color: $cursor;
    color:rgba(157, 157, 157, 1);
    font-size: 14px;
  }
}

/* reset element-ui css */
.login-container {
  background-color: rgba(43, 64, 85, 1) !important;
  .el-input {
    display: inline-block;
    input {
      background: transparent;
      border: none;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $bg;
      height: 47px;
      caret-color: $bg;

      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        box-shadow: 0 0 0px 1000px $cursor inset !important;
        -webkit-text-fill-color: $bg !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    // border-radius: 5px;
    color: #454545;

    background-color: rgba(255, 255, 255, 1);
    border-radius: 2px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;

  z-index: 5;
  background: url(https://lanhu.oss-cn-beijing.aliyuncs.com/psx1nf2lkzdb8ejwgqmbjppcwjz8466jh83f52736-1d28-4553-8bb0-c90082dbccbe)
    0px -181px no-repeat;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    text-align: center;
    font-size: 14px;
    color: rgba(255, 255, 255, 1);
    margin-bottom: 10px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    .group2 {
      background-color: rgba(255, 255, 255, 1);
      width: 52px;
      height: 52px;
      align-self: center;
    }
    .title {
      margin: 0px auto 40px auto;
      color: rgba(255, 255, 255, 1);
      font-size: 30px;
      margin-top: 20px;
      font-family: MicrosoftYaHei;
      white-space: nowrap;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 4px;
    font-size: 14px;
    color: #FF9800;
    cursor: pointer;
    user-select: none;
  }
}
</style>
